package com.dts.backproject.controller;

import com.dts.backproject.entity.po.Result;
import com.dts.backproject.entity.po.UserInfo;
import com.dts.backproject.service.UserInfoService;
import com.dts.backproject.util.COSUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传使用示例Controller
 * 这个类展示了如何使用腾讯云COS进行文件上传和URL存储
 */
@RestController
@RequestMapping("/example")
@CrossOrigin(origins = "*")
public class FileUploadExampleController {

    @Autowired
    private COSUploader cosUploader;
    
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 示例1: 上传用户头像并更新到数据库
     * 请求方式: POST
     * URL: /api/example/upload-avatar
     * 参数: file (MultipartFile), email (String)
     */
    @PostMapping("/upload-avatar")
    public Result uploadUserAvatar(@RequestParam("file") MultipartFile file, 
                                  @RequestParam("email") String email) {
        try {
            // 1. 上传文件到腾讯云COS
            String avatarUrl = cosUploader.uploadFile(file, "avatars");
            
            // 2. 更新用户头像URL到数据库
            userInfoService.updateAvatarUrl(email, avatarUrl);
            
            // 3. 返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("avatarUrl", avatarUrl);
            result.put("email", email);
            result.put("message", "头像上传并保存成功");
            
            return Result.success(result);
            
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("头像上传失败: " + e.getMessage());
        }
    }

    /**
     * 示例2: 上传文档文件
     * 请求方式: POST
     * URL: /api/example/upload-document
     * 参数: file (MultipartFile), category (String)
     */
    @PostMapping("/upload-document")
    public Result uploadDocument(@RequestParam("file") MultipartFile file,
                                @RequestParam(value = "category", defaultValue = "documents") String category) {
        try {
            // 上传文件到指定分类文件夹
            String fileUrl = cosUploader.uploadFile(file, category);
            
            Map<String, Object> result = new HashMap<>();
            result.put("fileUrl", fileUrl);
            result.put("category", category);
            result.put("originalName", file.getOriginalFilename());
            result.put("size", file.getSize());
            result.put("message", "文档上传成功");
            
            return Result.success(result);
            
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("文档上传失败: " + e.getMessage());
        }
    }

    /**
     * 示例3: 获取用户信息（包含头像URL）
     * 请求方式: GET
     * URL: /api/example/user-info?email=user@example.com
     */
    @GetMapping("/user-info")
    public Result getUserInfo(@RequestParam String email) {
        try {
            UserInfo userInfo = userInfoService.findByEmail(email);
            
            Map<String, Object> result = new HashMap<>();
            result.put("userInfo", userInfo);
            result.put("message", "获取用户信息成功");
            
            return Result.success(result);
            
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取用户信息失败: " + e.getMessage());
        }
    }
}

